package com.atguigu.guli.service.cms.controller.api;

import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.cms.entity.Ad;
import com.atguigu.guli.service.cms.service.AdService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

 //解决跨域问题
@Api(tags = "广告推荐显示")
@RestController
@RequestMapping("/api/cms/ad")
@Slf4j
public class ApiAdController {

    @Autowired
    private AdService adService;
    @ApiOperation("根据id获取推荐信息")
    @GetMapping("get/{id}")
    public R getById(@ApiParam(value = "推荐ID", required = true) @PathVariable String id) {
        Ad ad = adService.getById(id);
        if (ad != null) {
            return R.ok().data("item", ad);
        } else {
            return R.error().message("数据不存在");
        }
    }

    @ApiOperation("推荐分页列表")
    @GetMapping("list/{pageNum}/{pageSize}")
    public R listPage(@ApiParam(value = "当前页码", required = true) @PathVariable Long pageNum,
                      @ApiParam(value = "每页记录数", required = true) @PathVariable Long pageSize) {
        Page<Ad> page = new Page<>(pageNum,pageSize);
        adService.page(page);
        return R.ok().data("page",page);

    }

    @ApiOperation("推荐分页列表")
    @GetMapping("list")
    public R getAds(){
        Map<String,Object> map = adService.getAds();
        return R.ok().data(map);
    }

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @GetMapping("/set")
    public R setK(){
        redisTemplate.opsForValue().set("k1","v1");
        stringRedisTemplate.opsForValue().set("k2","v1");
        redisTemplate.opsForValue().increment("k3");
        return R.ok();
    }
}
